/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.drawee;

import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.ShowcaseApplication;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.facebook.imagepipeline.common.ImageDecodeOptionsBuilder;
import com.facebook.imagepipeline.common.RotationOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ListDialog;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DraweeRotationAbility extends Ability {
    private SimpleDraweeView mSimpleDraweeView;
    private Uri mUri;
    private Button mSpinner;
    private List<Entry> entries = new ArrayList<>();
    private int mSelectIdx= 0;

    private final Entry[] SPINNER_ENTRIES =
            new Entry[] {
                    new Entry(RotationOptions.disableRotation(), "disableRotation"),
                    new Entry(RotationOptions.autoRotate(), "autoRotate"),
                    new Entry(RotationOptions.autoRotateAtRenderTime(), "autoRotateAtRenderTime"),
                    new Entry(RotationOptions.forceRotation(RotationOptions.NO_ROTATION), "NO_ROTATION"),
                    new Entry(RotationOptions.forceRotation(RotationOptions.ROTATE_90), "ROTATE_90"),
                    new Entry(RotationOptions.forceRotation(RotationOptions.ROTATE_180), "ROTATE_180"),
                    new Entry(RotationOptions.forceRotation(RotationOptions.ROTATE_270), "ROTATE_270"),
            };
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_drawee_rotation);
        mSpinner = (Button) findComponentById(ResourceTable.Id_rotation_spinner);
        mSimpleDraweeView = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view);
        mUri = ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.M, ImageUriProvider.Orientation.ANY, ImageUriProvider.UriModification.NONE);
        entries.clear();
        Collections.addAll(entries,SPINNER_ENTRIES);
        mSpinner.setClickedListener((view)->{
            new ListDialog(view.getContext()){
                {
                    String[] items = new String[entries.size()];
                    entries.stream().flatMap((Function<Entry, Stream<?>>) entry -> Stream.of(entry.description)).collect(Collectors.toList()).toArray(items);
                    setSingleSelectItems(items,mSelectIdx);
                    setOnSingleSelectListener((iDialog, i) -> {
                        mSelectIdx = i;
                        Entry entry = entries.get(i);
                        setRotationOptions(entry.rotationOptions);
                        mSpinner.setText(entry.description);
                        iDialog.destroy();
                    });
                }
            }.show();
        });
    }
    private void setRotationOptions(RotationOptions rotationOptions) {
        ImageRequest imageRequest =
                ImageRequestBuilder.newBuilderWithSource(mUri)
                        .setRotationOptions(rotationOptions)
                        .setImageDecodeOptions(new ImageDecodeOptionsBuilder().build())
                        .build();
        mSimpleDraweeView.setImageRequest(imageRequest);
    }
    public static class Entry {

        final RotationOptions rotationOptions;
        final String description;

        private Entry(RotationOptions rotationOptions, String description) {
            this.rotationOptions = rotationOptions;
            this.description = description;
        }
    }
}
